<?php


namespace App\Http\Controllers\App;


use App\Exceptions\ErrorException;
use App\Exceptions\FailException;
use App\Exceptions\NotFoundException;
use App\Http\Controllers\Controller;
use App\Models\Store;
use App\Models\StoreCash;
use App\Models\StoreComment;
use App\Models\StoreDetail;
use DB;
use Exception;
use Auth;
use Log;

class StoreController extends Controller
{
    public function detail($id)
    {
        $store = Store::where('is_show', 1)->find($id);
        if (!$store) {
            throw new NotFoundException('店铺不存在');
        }
        $store->detail = StoreDetail::whereStoreId($store->id)->first()->message;
        $comment = StoreComment::where('store_id', $id)->get();
        foreach ($comment as &$value) {
            $value->images = json_encode(explode(',', $value->images));
        }
        return $store;
    }

    public function commentStore()
    {
        $this->validate(request(), [
            'content' => 'required',
            'images' => 'required',
            'score' => 'required',
            'price' => 'required'
        ]);
        $store_id = request('store_id');
        $store = Store::find($store_id);
        if (!$store) {
            throw new NotFoundException('店铺不存在');
        }
        try {
            DB::beginTransaction();
            $comment = new StoreComment();
            $comment->store_id = $store_id;
            $comment->member_id = Auth::id();
            $comment->content = request('content');
            $comment->images = request('images');
            $comment->score = request('score');
            $comment->price = request('price');
            if (request('parent_id')) {
                $comment->parent_id = request('parent_id');
            }
            if (!$comment->save()) {
                throw new FailException('评论失败');
            }
            //更新分数
            $commentCount = StoreComment::where('store_id', $store_id)->count();
            $scoreTotal = StoreComment::where('store_id', $store_id)->sum('score');
            $store->score = $scoreTotal / $commentCount;
            $store->save();
            $store->increment('comment_count');
            DB::commit();
        } catch (Exception $e) {
            DB::rollBack();
            Log::error($e);
            throw new ErrorException('评论失败');
        }

        return success('评论成功');
    }

    public function comment()
    {
        $store_id = request('store_id');
        $list = StoreComment::where('store_id', $store_id)
            ->select('m.avatar', 'm.name', 'store_comment.content', 'store_comment.images', 'store_comment.created_at', 'store_comment.parent_id')
            ->leftJoin('members as m', 'm.id', 'store_comment.member_id')
            ->get();
        return $list;
    }

    public function recommend()
    {
        $cate = request('category_id');
        $area = request('area');
        $sort = request('sort');
        $filter = request('filter');
        $lon = request('lon');
        $let = request('let');
        $select = ['id','name', 'background', 'area', 'discount', 'comment_count', 'score', 'category_id'];
        if ($lon && $let) {
            array_push($select, DB::raw(Store::distance($lon, $let)));
        }
        $stores = Store::select($select)->when($cate, function ($query, $cate) {
            $query->where('category_id', $cate);
        })->when($area, function ($query, $area) {
            $query->where('', $area);
        })->when($sort, function ($query, $sort) {

        })->when($filter, function ($query, $filter){

        })->inRandomOrder()->paginate();
        foreach ($stores->items() as &$item) {
            $item->background = explode(',', $item->background)[0];
        }
        return $stores;
    }

    // 商家体现测试
    public function reflect()
    {
        $store_cash = new StoreCash();
        $store_cash->store_id = 6;
        $store_cash->user_id = 1;
        $store_cash->money = 100;
        $store_cash->proportion = 0.1;
        $store_cash->status = 1;


        try {
            DB::beginTransaction();
            $store_cash->save();
            StoreCash::serviceCharge($store_cash->id);
            DB::commit();
        } catch (Exception $exception) {
            DB::rollBack();
            echo $exception;
        }
    }

    public function commentKeys()
    {
        return ['味道不错'];
    }
}